package com.bite.java_chatroom.controller;

import com.bite.java_chatroom.mapper.FriendMapper;
import com.bite.java_chatroom.model.MessageResp;
import com.bite.java_chatroom.model.UserInfo;
import com.bite.java_chatroom.service.FriendService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/friend")
public class FriendController {
    @Autowired
    private FriendService friendService;

    @Autowired
    private FriendMapper friendMapper;

    @RequestMapping("/getList")
    public List<UserInfo> getFriendList(HttpSession session){
        if(session == null){
            log.info("[getFriendLsit] session 不存在");
            return new ArrayList<>();
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[getFriendLsit] user 不存在");
            return new ArrayList<>();
        }
        return friendService.getFriendList(userInfo.getId());
    }

    @RequestMapping("/judgfriend")
    public boolean selectFriendByUserId(Integer friendId,HttpSession session){
        if(session == null){
            log.info("[selectFriendByUserId] session 不存在");
            return false;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0) {
            log.info("[selectFriendByUserId] user 不存在");
            return false;
        }
        int ret = friendMapper.selectFriendByUserId(userInfo.getId(),friendId);
        if(ret > 0){
            return true;
        }
        return false;
    }

    @RequestMapping("/searchFriends")
    public List<UserInfo> searchFriends(String userName, HttpSession session){
        if(session == null){
            log.info("[searchFriends] session 不存在");
            return null;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[searchFriends] user 不存在");
            return null;
        }
        List<UserInfo> Friends =  friendService.searchFriends(userName,userInfo.getId());
        System.out.println(Friends.size());
        return Friends.size()== 0 ? null : Friends;
    }

    @RequestMapping("/reject")
    public boolean removeAddFriend(Integer fromId, HttpSession session){
        if(session == null){
            log.info("[getFriendLsit] session 不存在");
            return false;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[getFriendLsit] user 不存在");
            return false;
        }
        friendService.removeAddFriend(fromId,userInfo.getId());
        return true;
    }

    @RequestMapping("/add")
    public boolean addFriend(Integer fromId,HttpSession session){
        if(session == null){
            log.info("[getFriendLsit] session 不存在");
            return false;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[getFriendLsit] user 不存在");
            return false;
        }

        friendService.addFriend(fromId,userInfo.getId());
        return true;
    }

    @RequestMapping("/getAddFriend")
    public List<MessageResp> getAddFriend(HttpSession session){
        if(session == null){
            log.info("[getFriendLsit] session 不存在");
            return null;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[getFriendLsit] user 不存在");
            return null;
        }
        List<MessageResp> respList =  friendService.selectAddFriends(userInfo.getId());
        if(respList.size() != 0){
            for (MessageResp resp : respList){
                resp.setType("addFriend");
            }
        }
        return respList;
    }

    @RequestMapping("/setState")
    public boolean setAddFriendState(HttpSession session){
        if(session == null){
            log.info("[setAddFriendState] session 不存在");
            return false;
        }
        UserInfo userInfo = (UserInfo) session.getAttribute("session_user_info");
        if(userInfo == null || userInfo.getId() == 0){
            log.info("[setAddFriendState] user 不存在");
            return false;
        }
        Integer ret =friendMapper.setAddFriendState(userInfo.getId());
        if(ret>0){
            return true;
        }
        return false;
    }
}
